package com.rzico.sbl.ui.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.rzico.sbl.R
import com.rzico.sbl.databinding.FragmentMineBinding
import com.rzico.sbl.other.driverEnable
import com.rzico.sbl.other.userRole
import com.rzico.sbl.ui.WebActivity
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.phoneReplaceWithStar
import com.xinnuo.common_ui.base.NavigationFragment
import com.xinnuo.common_ui.glideExt.loadCircleImage
import com.xinnuo.common_ui.utils.showBottomPhoneDialog
import org.json.JSONObject

class MineFragment : NavigationFragment() {

    private var mBinding: FragmentMineBinding? = null

    override fun getViewModel() = get<MineViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = FragmentMineBinding
        .inflate(inflater, container, false)
        .also { mBinding = it }
        .root

    override fun initTitle() {
        initListener()

        when (userRole) {
            "1", "2" -> {
                // 是否纯司机角色
                if (driverEnable) {
                    mBinding?.mineInfoLl2?.visible()
                    mBinding?.mineInfoLl?.gone()
                    mBinding?.mineShopQrcode?.gone()
                    mBinding?.mineBill?.gone()
                } else {
                    mBinding?.mineShop?.visible()
                }
            }
            "5", "6", "7", "8", "9", "11", "13" -> {
                mBinding?.mineInfoLl2?.visible()
                mBinding?.mineInfoLl?.gone()
                mBinding?.mineShopQrcode?.gone()
                mBinding?.mineBill?.gone()
            }
        }

        getData()
    }

    private fun initListener() {
        mBinding?.let {
            it.mineInfoLl.oneClick { startActivityEx<InfoActivity>() }
            it.mineInfoLl2.oneClick { startActivityEx<InfoActivity>() }
            it.mineInfo.oneClick { startActivityEx<InfoActivity>() }
            it.mineShopQrcode.oneClick { startActivityEx<ShopCodeActivity>() }
            it.mineImg.oneClick { startActivityEx<InfoActivity>() }
            it.mineShop.oneClick { startActivityEx<ShopActivity>() }
            it.mineBill.oneClick { startActivityEx<WalletActivity>() }
            it.mineFeedback.oneClick { startActivityEx<FeedbackActivity>() }
            it.mineDeal.oneClick { startActivityEx<WebActivity>("title" to "用户协议") }
            it.mineSecret.oneClick { startActivityEx<WebActivity>("title" to "隐私政策") }
            it.mineSetting.oneClick { startActivityEx<SettingActivity>() }
            it.mineService.oneClick {
                requireContext()
                    .showBottomPhoneDialog(getString(R.string.phone_service))
            }
        }
    }

    /**
     * 获取个人信息
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .mInfoEvent
            .observe(this) { result ->
                val obj = JSONObject(result)

                mBinding?.let {
                    val logo = obj.optNotNull("logo")
                    val name = obj.optNotNull("nickName")
                    val phone = obj.optNotNull("mobile").phoneReplaceWithStar()

                    when (userRole) {
                        "5", "6", "7", "8", "9", "11", "13" -> {
                            setUserInfo2(logo, name, phone)
                        }
                        "1", "2" -> {
                            // 是否纯司机角色
                            if (driverEnable) setUserInfo2(logo, name, phone)
                            else setUserInfo(logo, name, phone)
                        }
                        else -> setUserInfo(logo, name, phone)
                    }
                }
            }
    }

    /**
     * 设置第一个人信息
     */
    private fun setUserInfo(logo: String, name: String, phone: String) {
        mBinding?.let {
            it.mineName.text = name
            it.minePhone.text = "手机号：$phone"

            when {
                it.mineImg.getTag(R.id.empty_img) == null -> {
                    it.mineImg.loadCircleImage(logo)
                    it.mineImg.setTag(R.id.empty_img, logo)
                }
                it.mineImg.getTag(R.id.empty_img) != logo -> {
                    it.mineImg.loadCircleImage(logo)
                    it.mineImg.setTag(R.id.empty_img, logo)
                }
            }
        }
    }

    /**
     * 设置第二个人信息
     */
    private fun setUserInfo2(logo: String, name: String, phone: String) {
        mBinding?.let {
            it.mineName2.text = name
            it.minePhone2.text = phone

            when {
                it.mineImg2.getTag(R.id.empty_img) == null -> {
                    it.mineImg2.loadCircleImage(logo)
                    it.mineImg2.setTag(R.id.empty_img, logo)
                }
                it.mineImg2.getTag(R.id.empty_img) != logo -> {
                    it.mineImg2.loadCircleImage(logo)
                    it.mineImg2.setTag(R.id.empty_img, logo)
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()

        getViewModel().mineInfo()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding = null
    }

}